package com.huawei.esdk.tp.device.smc.v100r003c10.convert;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.datatype.XMLGregorianCalendar;

import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.utils.DateUtils;
import com.huawei.esdk.platform.common.utils.DurationUtils;
import com.huawei.esdk.tp.domain.model.bean.MCUInfo;
import com.huawei.esdk.tp.domain.model.bean.MCUInfoList;
import com.huawei.esdk.tp.domain.model.bean.MCUResource;
import com.huawei.esdk.tp.domain.model.bean.MCUResourceItem;
import com.huawei.esdk.tp.domain.model.bean.MCUResourceList;
import com.huawei.esdk.tp.domain.model.bean.PagesInfo;
import com.huawei.esdk.tp.domain.model.bean.QueryCondition;
import com.huawei.esdk.tp.domain.model.bean.QuerySites;
import com.huawei.esdk.tp.domain.model.bean.SortField;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.ArrayOfFilterBase;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.ArrayOfMcuInfo;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.ArrayOfMcuResource;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.ArrayOfMcuResourceItem;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.ArrayOfResourceOccupiedStatus;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.ArrayOfSortItem;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.ArrayOfUnsignedInt;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.DateTimeFilter;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.DateTimeRange;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.DoubleFilter;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.FilterBase;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.FilterColumn2;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.IPAddressEx;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.IPAddressFilter;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.IntFilter;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.McuInfo;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.McuResource;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.McuResourceItem;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.PageParam;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.QueryConfig;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.QueryMcuFutureResource;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.QueryMcuFutureResourceResponse;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.QueryMcuInfo;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.QueryMcuInfoResponse;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.ResourceOccupiedStatus;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.SortItem;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.SortType;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.StringFilter;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.TimeSpanEx;

public class TPSMC20V1R3C10MCUMgrConvert
{
    
    public QueryMcuInfo queryMCUInfoModel2Soap(QuerySites querySites)
    {
        List<SortField> sortFields = querySites.getSortField();
        ArrayOfSortItem arrayOfSortItem = new ArrayOfSortItem();
        SortItem sortItem = null;
        
        if (null != sortFields && !sortFields.isEmpty())
        {
            for (SortField sortField : sortFields)
            {
                sortItem = new SortItem();
                Integer isAscend = sortField.getIsAscend();
                Integer fieldIndex = sortField.getFieldIndex();
                
                // DTS2015012305074 
                // 非必填参数不为空时，北向转换校验其必填属性
                sortItem.setSort(getSortTypeInt2Enum(isAscend));
                sortItem.setItemIndex(filterColumn2Int2Enum(fieldIndex));
                
                arrayOfSortItem.getSortItem().add(sortItem);
            }
            
        }
        
        List<QueryCondition> queryConditions = querySites.getQueryCondition();
        ArrayOfFilterBase filters = new ArrayOfFilterBase();
        FilterBase filterBase = null;
        
        if (null != queryConditions && !queryConditions.isEmpty())
        {
            for (QueryCondition queryCondition : queryConditions)
            {
                filterBase = getFilterModal2Soap(queryCondition);
                filters.getFilterBase().add(filterBase);
            }
        }
        
        Long focusItem = querySites.getFocusItem();
        
        PageParam pageParam = new PageParam();
        pageParam.setCurrentPage(querySites.getCurrentPage());
        pageParam.setNumberPerPage(querySites.getNumberPerPage());
        
        QueryConfig queryConfig = new QueryConfig();
        queryConfig.setFilters(filters);
        queryConfig.setFocusItem(focusItem);
        queryConfig.setPageParam(pageParam);
        queryConfig.setSortItems(arrayOfSortItem);
        
        QueryMcuInfo resMsg = new QueryMcuInfo();
        resMsg.setQueryConfig(queryConfig);
        
        return resMsg;
    }
    
    private FilterBase getFilterModal2Soap(QueryCondition item)
    {
        
        if (null == item)
        {
            return null;
        }
        if ("1".equals(item.getConditionType()))
        {
            if (2 == item.getConditionValue().length)
            {
                StringFilter stringFilter = new StringFilter();
                stringFilter.setColumnIndex(filterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                stringFilter.setValue((String)item.getConditionValue()[1]);
                return stringFilter;
            }
            else
            {
                return null;
            }
        }
        else if ("2".equals(item.getConditionType()))
        {
            if (4 == item.getConditionValue().length)
            {
                IntFilter filter = new IntFilter();
                filter.setColumnIndex(filterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                filter.setGreaterThanValue((Integer)item.getConditionValue()[1]);
                filter.setLessThanValue((Integer)item.getConditionValue()[2]);
                filter.setEqualToValue((Integer)item.getConditionValue()[3]);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else if ("3".equals(item.getConditionType()))
        {
            if (4 == item.getConditionValue().length)
            {
                DoubleFilter filter = new DoubleFilter();
                filter.setColumnIndex(filterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                filter.setGreaterThanValue((Double)item.getConditionValue()[1]);
                filter.setLessThanValue((Double)item.getConditionValue()[2]);
                filter.setEqualToValue((Double)item.getConditionValue()[3]);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else if ("4".equals(item.getConditionType()))
        {
            if (3 == item.getConditionValue().length)
            {
                DateTimeFilter filter = new DateTimeFilter();
                filter.setColumnIndex(filterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                filter.setGreaterThanValue((XMLGregorianCalendar)item.getConditionValue()[1]);
                filter.setLessThanValue((XMLGregorianCalendar)item.getConditionValue()[2]);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else if ("5".equals(item.getConditionType()))
        {
            if (2 == item.getConditionValue().length)
            {
                IPAddressFilter filter = new IPAddressFilter();
                filter.setColumnIndex(filterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                IPAddressEx value = new IPAddressEx();
                value.setIPAddress((String)item.getConditionValue()[0]);
                filter.setValue(value);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }
        
    }
    
    //findbugs 方法名首字母小写
    private FilterColumn2 filterColumn2Int2Enum(Integer val)
    {
        switch (val)
        {
            case 0:
                return FilterColumn2.SITE_NAME;
            case 1:
                return FilterColumn2.SITE_URI;
            case 2:
                return FilterColumn2.SITE_STATUS;
            case 3:
                return FilterColumn2.SITE_TYPE;
            case 4:
                return FilterColumn2.CONF_TEMPLATE_NAME;
            case 5:
                return FilterColumn2.CONF_TEMPLATE_ACCESS_CODE;
            case 6:
                return FilterColumn2.MCU_NAME;
            default:
                return null;
        }
        
    }
    
    // DTS2015012305074 设置sort除0和1外的值时默认返回null
    private SortType getSortTypeInt2Enum(Integer val)
    {
        switch (val)
        {
            case 0:
                return SortType.ASC;
            case 1:
                return SortType.DESC;
            default:
                return null;
        }
    }
    
    /*public static String getSort(Integer n)
    {
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(0, "asc");
        map.put(1, "desc");
        
        return map.get(n);
    }*/
    
    /*public String getIndex(Integer n)
    {
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(0, "SiteName");
        map.put(1, "SiteUri");
        map.put(2, "SiteStatus");
        map.put(3, "SiteType");
        map.put(4, "ConfTemplateName");
        map.put(5, "ConfTemplateAccessCode");
        map.put(6, "McuName");
        
        return map.get(n);
    }*/
    
    public SDKResult<MCUInfoList> queryMCUInfoSoap2Model(QueryMcuInfoResponse response)
    {
        SDKResult<MCUInfoList> result = new SDKResult<MCUInfoList>();
        result.setErrCode(response.getQueryMcuInfoResult().getErrCode());
        
        ArrayOfMcuInfo mcus = response.getMcus();
        MCUInfoList mcuConferencesList = new MCUInfoList();
        if (null != mcus)
        {
            
            List<McuInfo> mcuInfos = mcus.getMcuInfo();
            MCUInfo mcuInfoEx = null;
            //findbugs 多余的判空检查
            if (!mcuInfos.isEmpty())
            {
                for (McuInfo mcuInfo : mcuInfos)
                {
                    if (null == mcuInfo.getIp1())
                    {
                        mcuInfo.setIp1(new IPAddressEx());
                    }
                    
                    if (null == mcuInfo.getIp2())
                    {
                        mcuInfo.setIp2(new IPAddressEx());
                    }
                    
                    mcuInfoEx = new MCUInfo();
                    mcuInfoEx.setDomainName(mcuInfo.getDomainName());
                    mcuInfoEx.setId((int)mcuInfo.getId());
                    mcuInfoEx.setIp1(mcuInfo.getIp1().getIPAddress());
                    mcuInfoEx.setIp2(mcuInfo.getIp2().getIPAddress());
                    mcuInfoEx.setName(mcuInfo.getName());
                    
                    if (mcuInfo.getPort1() != null)
                    {
                        mcuInfoEx.setPort1(mcuInfo.getPort1());
                    }
                    
                    if (mcuInfo.getPort2() != null)
                    {
                        mcuInfoEx.setPort2(mcuInfo.getPort2());
                    }
                    
                    mcuInfoEx.setStatus(getStatusStr2Int(mcuInfo.getStatus().value()));
                    mcuInfoEx.setType(getTypeStr2Int(mcuInfo.getType().value()));
                    
                    mcuConferencesList.getMcuInfos().add(mcuInfoEx);
                }
            }
            
        }
        
        com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c10.PagesInfo pagesInfoSoap = response.getPageInfo();
        
        if (null != pagesInfoSoap)
        {
            PagesInfo pagesInfo = new PagesInfo();
            pagesInfo.setCurrentPage(pagesInfoSoap.getCurrentPage());
            pagesInfo.setTotalPages(pagesInfoSoap.getTotalPages());
            pagesInfo.setTotalRecords(pagesInfoSoap.getTotalRecords());
            
            mcuConferencesList.setPagesInfo(pagesInfo);
        }
        
        result.setResult(mcuConferencesList);
        
        return result;
    }
    
    private Integer getTypeStr2Int(String value)
    {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("Mcu9660", 0);
        map.put("Mcu9650", 1);
        map.put("Mcu9630", 2);
        map.put("Mcu8660", 3);
        map.put("Mcu8650", 4);
        map.put("Mcu8650C", 5);
        
        return map.get(value);
    }
    
    private Integer getStatusStr2Int(String value)
    {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("Online", 0);
        map.put("Offline", 1);
        
        return map.get(value);
    }
    
    public QueryMcuFutureResource queryMCUFutureResourceModel2Soap(Integer duration, Date beginTime, List<Integer> mcus)
    {
        DateTimeRange dateTimeRange = new DateTimeRange();
        dateTimeRange.setStart(DateUtils.setTimezoneToUTC(DateUtils.toGregorianCalendarDate(beginTime), 0));
        TimeSpanEx timeSpanExSouth = new TimeSpanEx();
        timeSpanExSouth.setTimeSpan("PT" + duration + "M");
        dateTimeRange.setSpan(timeSpanExSouth);
        
        QueryMcuFutureResource resMsg = new QueryMcuFutureResource();
        resMsg.setTimeRange(dateTimeRange);
        
        if (null != mcus && !mcus.isEmpty())
        {
            resMsg.setMcus(new ArrayOfUnsignedInt());
            for (Integer mcu : mcus)
            {
                //DTS2015012603196 mcus为空，查未来时间MCU的资源占用情况，返回2130000010错误
                //在北向加上判断，如果必填参数为空，则返回错误码
                int i = mcu;
                long l = i;
                
                resMsg.getMcus().getUnsignedInt().add(l);
            }
        }
        
        return resMsg;
    }
    
    public SDKResult<MCUResourceList> queryMCUFutureResourceSoap2Model(QueryMcuFutureResourceResponse response)
    {
        SDKResult<MCUResourceList> result = new SDKResult<MCUResourceList>();
        result.setErrCode(response.getQueryMcuFutureResourceResult().getErrCode());
        
        MCUResourceList mcuResourceList = new MCUResourceList();
        
        ArrayOfMcuResource mcuResList = response.getMcuResList();
        if (null != mcuResList)
        {
            List<McuResource> mcuResources = mcuResList.getMcuResource();
            
          // findbugs 多余的判空检查
            if (!mcuResources.isEmpty())
            {
                MCUResource mcuResourceDomain = null;
                for (McuResource mcuResource : mcuResources)
                {
                    mcuResourceDomain = new MCUResource();
                    mcuResourceDomain.setMcuId((int)mcuResource.getMcuId());
                    
                    ArrayOfMcuResourceItem resourceList = mcuResource.getResourceList();
                    if (null != resourceList)
                    {
                        List<McuResourceItem> mcuResourceItems = resourceList.getMcuResourceItem();
                        
                      // findbugs 多余的判空检查
                        if (!mcuResourceItems.isEmpty())
                        {
                            MCUResourceItem resourceListDomain = null;
                            for (McuResourceItem mcuResourceItem : mcuResourceItems)
                            {
                                resourceListDomain = new MCUResourceItem();
                                resourceListDomain.setResourceType(getTypeString2Int(mcuResourceItem.getResourceType()
                                    .value()));
                                ArrayOfResourceOccupiedStatus resourceStatus = mcuResourceItem.getResourceStatus();
                                if (null != resourceStatus)
                                {
                                    List<ResourceOccupiedStatus> resourceOccupiedStatuss =
                                        resourceStatus.getResourceOccupiedStatus();
                                  // findbugs 多余的判空检查
                                    if (!resourceOccupiedStatuss.isEmpty())
                                    {
                                        com.huawei.esdk.tp.domain.model.bean.ResourceOccupiedStatus resourceStatusDomain =
                                            null;
                                        for (ResourceOccupiedStatus resourceOccupiedStatus : resourceOccupiedStatuss)
                                        {
                                            resourceStatusDomain =
                                                new com.huawei.esdk.tp.domain.model.bean.ResourceOccupiedStatus();
                                            resourceStatusDomain.setBeginTime(resourceOccupiedStatus.getTimeRange()
                                                .getStart());
                                            resourceStatusDomain.setDuration(DurationUtils.durationStr2dur(resourceOccupiedStatus.getTimeRange()
                                                .getSpan()
                                                .getTimeSpan()));
                                            resourceStatusDomain.setFreeCount(resourceOccupiedStatus.getFreeCount());
                                            resourceStatusDomain.setTotalCount(resourceOccupiedStatus.getTotalCount());
                                            
                                            resourceListDomain.getResourceStatus().add(resourceStatusDomain);
                                        }
                                    }
                                }
                                mcuResourceDomain.getResourceList().add(resourceListDomain);
                            }
                        }
                        
                    }
                    
                    mcuResourceList.getMcuResource().add(mcuResourceDomain);
                }
                result.setResult(mcuResourceList);
            }
        }
        
        return result;
    }
    
    private static Integer getTypeString2Int(String value)
    {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("ContinuousPresence", 0);
        map.put("RateAdpter", 1);
        map.put("Capacity", 2);
        map.put("VideoPort", 3);
        map.put("AudioPort", 4);
        
        return map.get(value);
    }
    
}
